<template>
  <div>
    <el-button type="primary" size="medium" plain @click="eject"
      >添加</el-button
    >
    <v-list @edit="edit" :list="list" @init="initandtotal"></v-list>

    <!-- 分页 -->
    <!-- 分页组件 
    total:总数
    page-size：一页的条数
     @current-change:当前页码发生了改变，event就是新的页码
    -->

    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :page-size="size"
      @current-change="changePage"
    >
    </el-pagination>

    <v-dialog
      :eject="info"
      @close="close"
      @init="initandtotal"
      @update="init"
      :list="list"
      ref="form"
    ></v-dialog>
  </div>
</template>

<script>
import vList from "./components/list";
import vDialog from "./components/dialog";
import { requserlist, requserall } from "../../request/api";
export default {
  components: {
    vList,
    vDialog,
  },
  data() {
    return {
      total: 0,
      size: 2,
      page: 1,
      info: {
        isshow: false,
        isadd: false,
      },
      list: [],
    };
  },
  created() {},
  mounted() {
    this.initandtotal();
  },
  methods: {
    initandtotal() {
      //   重新刷新页面
      this.init();
      // 管理员总数获取
      this.gettotal();
    },
    //   获取总数方法
    gettotal() {
      requserall().then((res) => {
        this.total = res.data.list[0].total;
      });
    },
    eject() {
      this.info.isshow = true;
      this.info.isadd = true;
    },
    close() {
      this.info.isshow = false;
    },
    edit(id) {
      this.info.isshow = true;
      this.info.isadd = false;
      this.$refs.form.getone(id);
    },
    // 请求数据
    init() {
      requserlist({ size: this.size, page: this.page }).then((res) => {
        if (res.data.code == 200) {
          let list = res.data.list ? res.data.list : [];
          if (list.length == 0 && this.page !== 1) {
            this.page--;
            this.init();
            return;
          }
          this.list = list;
        }
      });
    },
    // 页面变化了
    changePage(e) {
      console.log(e);
      this.page = e;

      this.init();
    },
  },
};
</script>

<style scoped >
.el-button {
  margin-bottom: 20px;
}
</style>
